var PrimoWikiInputMethod = InputMethod.extend({
	/*variables defination*/
	result		: '',
	strTemp		: '',
	constructor	: function() {},
	IMName		: function() {return 'PrimoWiki';},
	
	/*methods*/ 
	_aCombiner: function(href, title, text)
	{
		return "<a href='" +href +  "' title='" + title + "'>" + text + "</a>";
	} // end of _aCombiner
	,
	_formatters: function(str)
	{
		/* 
		str is the original preeedit from IME without any modification
		strTemp is used to store the formatted str
		*/

		strTemp = str;
		
		/*text formatting*/
		if(this._BoldFilter(strTemp)){
			strTemp = this._BoldFormatter(strTemp);
		}
		if(this._ItalicFilter(strTemp)){
			strTemp = this._ItalicFormatter(strTemp);
		}
		if(this._InsertedFilter(strTemp)){
			strTemp = this._InsertedFormatter(strTemp);
		}
		if(this._DeletedFilter(strTemp)){
			strTemp = this._DeletedFormatter(strTemp);
		}
		if(this._SuperScriptFilter(strTemp)){
			strTemp = this._SuperScriptFormatter(strTemp);
		}
		if(this._SubScriptFilter(strTemp)){
			strTemp = this._SubScriptFormatter(strTemp);
		}
		if(this._FontColorFilter(strTemp)){
			strTemp = this._FontColorFormatter(strTemp);
		}
		if(this._FontBgColorFilter(strTemp)){
			strTemp = this._FontBgColorFormatter(strTemp);
		}
		/*heading*/
		if(this._HeadingFilter(strTemp)){
			strTemp = this._HeadingFormatter(strTemp);
		}

		/*quotation*/
		if(this._QuotationFilter(strTemp)){
			strTemp = this._QuotationFormatter(strTemp);
		}
		/*
		if(this._ListFilter(str)){

		}else if(this._DeletedTextFilter(str)){
		}else if(this._DirectLinkFilter(str)){
		}else if(this._SisterWikiFilter(str)){
		}else if(this._WikiLinkFilter(str)){
		}*/

		/*return the result*/
		this.result = strTemp;
		return true;
	} /*end of _Convert*/
	,
	/*
	---------------
	text formatting
	---------------
	*/
	/*Bold Text*/
	_BoldFilter: function(str){
		if(str.match("''")==null) {
			return false;
		}else{
			if(str.indexOf("''")!=str.lastIndexOf("''")){
				return true;
			}
		}
	}
	,
	_BoldFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("''");
		var lastOccurrence = str.lastIndexOf("''");
		return str.substr(0,firstOccurrence) + "<b>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</b>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	/*Italic Text*/
	_ItalicFilter: function(str)
	{
		if(str.match("//")==null) {
			return false;
		}else{
			if(str.indexOf("//")!=str.lastIndexOf("//")){
				return true;
			}
		}
	}
	,
	_ItalicFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("__");
		var lastOccurrence = str.lastIndexOf("__");
		return str.substr(0,firstOccurrence) + "<i>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</i>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	/*Inserted Text*/
	_InsertedFilter: function(str)
	{
		if(str.match("__")==null) {
			return false;
		}else{
			if(str.indexOf("__")!=str.lastIndexOf("__")){
				return true;
			}
		}
	}
	,
	_InsertedFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("__");
		var lastOccurrence = str.lastIndexOf("__");
		return str.substr(0,firstOccurrence) + "<ins>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</ins>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	/*Deleted Text*/
	_DeletedFilter: function(str)
	{
		if(str.match("==")==null) {
			return false;
		}else{
			if(str.indexOf("==")!=str.lastIndexOf("==")){
				return true;
			}
		}
	}
	,
	_DeletedFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("==");
		var lastOccurrence = str.lastIndexOf("==");
		return str.substr(0,firstOccurrence) + "<del>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</del>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	_SuperScriptFilter: function(str)
	{
		if(str.match("^^")==null) {
			return false;
		}else{
			if(str.indexOf("^^")!=str.lastIndexOf("^^")){
				return true;			
			}
		}
	} 
	,
	_SuperScriptFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("^^");
		var lastOccurrence = str.lastIndexOf("^^");
		return str.substr(0,firstOccurrence) + "<sup>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</sup" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	_SubScriptFilter: function(str)
	{
		if(str.match("~~")==null) {
			return false;
		}else{
			if(str.indexOf("~~")!=str.lastIndexOf("~~")){
				return true;
			}
		}
	}
	,
	_SubScriptFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("~~");
		var lastOccurrence = str.lastIndexOf("~~");
		return str.substr(0,firstOccurrence) + "<sub>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</sub>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	_FontColorFilter: function(str)
	{
		if(!(str.match("@@color(")==null) && !(str.match(")")==null) ){
			if(str.indexOf("@@color(")!=str.lastIndexOf("@@")){
				return true;
			}else {
				return false;
			}
		}else{
			return false;
		}
	}
	,
	_FontColorFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("@@");
		var lastOccurrence = str.lastIndexOf("@@");
		var color = str.indexOf("@@color(")+8,str.indexOf(")")-(str.indexOf("@@color(")+8);
		return str.substr(0,firstOccurrence) 
			+ "<font color='" + color + "'>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</font>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,

	_FontBgColorFilter: function(str)
	{
		if(!(str.match("@@bgcolor(")==null) && !(str.match(")")==null) ){
			if(str.indexOf("@@bgcolor(")!=str.lastIndexOf("@@")){
				return true;
			}else {
				return false;
			}
		}else{
			return false;
		}
	}
	,
	_FontBgColorFormatter: function(str)
	{
		var firstOccurrence = str.indexOf("@@");
		var lastOccurrence = str.lastIndexOf("@@");
		var bgcolor = str.indexOf("@@bgcolor(")+10,str.indexOf(")")-(str.indexOf("@@bgcolor(")+10);
		return str.substr(0,firstOccurrence) 
			+ "<font bgcolor='" + bgcolor + "'>" 
			+ str.substr(firstOccurrence+2,lastOccurrence-firstOccurrence-2) + "</font>" 
			+ str.substr(lastOccurrence+2,str.length);
	}
	,


	/*
	---------------
	heading
	---------------
	*/

	_HeadingFilter: function(str){
		if(str.charAt(0)=="!") {
			return true;
		}else{
			return false;
		}
	}
	,
	_HeadingFormatter: function(str)
	{
		if(str.charAt(0)=="!" && str.charAt(1)!="!"){
			return "<h1>" + str.substr(1) + "</h1>";
		}else if(str.charAt(0)=="!" && str.charAt(1)=="!" && str.charAt(2)!="!"){
			return "<h2>" + str.substr(2) + "</h2>";
		}else if(str.charAt(0)=="!" && str.charAt(1)=="!" && str.charAt(2)=="!" && str.charAt(3)!="!"){
			return "<h3>" + str.substr(3) + "</h3>";
		}else if(str.charAt(0)=="!" && str.charAt(1)=="!" && str.charAt(2)=="!" && str.charAt(3)=="!" && str.charAt(4)!="!"){
			return "<h4>" + str.substr(4) + "</h4>";
		}else if(str.charAt(0)=="!" && str.charAt(1)=="!" && str.charAt(2)=="!" && str.charAt(3)=="!" && str.charAt(4)=="!"){
			return "<h5>" + str.substr(5) + "</h5>";
		}
	}


	,QuotationFilter: function(str){
		if(str.charAt(0)==">") {
			return true;
		}else{
			return false;
		}
	}
	,
	_QuotationFilterFormatter: function(str)
	{

	}
	,




	_ListFilter: function(str)
	{
		// ^
		if(str.charAt(0)=="*" && str.charAt(1)==" " && str.charAt(2)==" ") {
			this.result = "<ul><li>" + str.substr(str.indexOf('*  ')+2,str.length) + "</li></ul>";
			return true;
		}else return false;
	} // end of _SuberScriptFilter
	,

	_DirectLinkFilter: function(str)
	{
		//*ajaxime.com
		if(str.charAt(0) == "*" ){
			this.result = this._aCombiner(str.substr(1), str.substr(1), str.substr(1));
			return true;
		}
		else return false;
	} // end of _DirectLinkFilter
	,
	_WikiLinkFilter: function(str)
	{
		//[[www.ajaxime.com]]
		if(str.indexOf('[[')==-1||str.indexOf(']]')==-1){
			return false;
		}
		//[[www.ajaxime.com|ajaxime]]
		else if(str.indexOf('|')!=-1){
			this.result = this._aCombiner(str.substr(2,str.indexOf('|')-2),str.substr(str.indexOf('|')+1,str.length-4),str.substr(str.indexOf('|')+1,str.length-4));
			return true;
		}
		//[[www.ajaxime.com]]
		else if(str.charAt(0) == "[[" && str.charAt(1) == "[[" && str.charAt(str.length-1) == "]]" && str.charAt(str.length-2) == "]]"){
			this.result = this._aCombiner(str.substr(2,str.length-4), str.substr(2,str.length-4), str.substr(2,str.length-4));
			return true;
		}
		else return false;
	} // end of _WikiLinkFilter
	,
	_SisterWikiFilter: function(str)
	{
		if(str.indexOf(':')==-1){
			return false;
		}
		//******* need a adddress analyzer here*******
		
		// ajaxime::
		else if(str.charAt(str.length-1) == ":" && str.charAt(str.length-2) == ":"){
			this.result = this._aCombiner(str.substr(0,str.length-2), str.substr(0,str.length-2), str.substr(0,str.length-2));
			return true;
		} 
		//ajaxime::http://www.ajaxime.com
		/*
		if(str.charAt(str.indexOf(':')+1) == ":"){
			this.result = this._aCombiner(str.substr(i+2,str.length-i-1)), str.substr(0,i+1), str.substr(0,i+1));
			return true;
		}*/
		else return false;
	} // end of _SisterWikiFilter
	,
	keydown_handler: function(obj,key) 
	{
		if(key == 8) {
			if(this.ime.GetPreeditLength() > 0) {
				this.ime.DeletePreedit(1,1);
				return false;
			}
			else return true;
		}
		/*else if(key == 190) {
			this.ime.AppendPreedit('.','.');
			return false;
		}*/
		return true;
	} // end of keydown_handler
	,
	keypress_handler: function(obj,key) 
	{
		if(key == 27) { // escape
			this.ime.ResetPreedit();
			return true;
		}
		/*else if(key == 32) { // press "space bar" mean complete
			var preedit = this.ime.GetPreedit();
			if(this._formatters(preedit)) {
				this.ime.ResetPreedit();
				this.ime.SendHTML(obj,this.result);
				//this.ime.SendString(obj,this.result);
			}
			else this.ime.InvalidatePreedit();
			return false;
		}*/
		else if(key == 32) { //"space bar" 
			var ch = String.fromCharCode(key);
			this.ime.AppendPreedit(ch,ch);
			return false;
		}
		else if(key == 32 && (this.ime.GetPreedit()!=null||this.ime.GetPreedit()this.ime.GetPreedit()!="";)) { // press "enter" mean complete
			var preedit = this.ime.GetPreedit();
			if(this._formatters(preedit)) {
				this.ime.ResetPreedit();
				this.ime.SendHTML(obj,this.result);
				//this.ime.SendString(obj,this.result);
			}
			else this.ime.InvalidatePreedit();
			return false;
		}
		else if(key != 8 && key != 190){
			var ch = String.fromCharCode(key);
			this.ime.AppendPreedit(ch,ch);
			return false;
		}
		else return true;
	}
});


